<?php

//los datos del form:
$accion = $_POST['ACCION'];
$id_articulo = $_POST['id'];

switch ($accion) {
  case "ADD":

    // ARTICULOS Y SUBNIVEL 2
    if (isset($_POST['subnivel2']) && $_POST['subnivel2'] != '-1') {

      $id_subnivel2 = $_POST['subnivel2'];

      require_once('../php/class/articulo_subnivel2.class.php');
      $oArNiv2 = new articulos_subnivel2();

      $oArNiv2->ID = $oArNiv2->getlastid();
      $oArNiv2->ID_ARTICULO = $id_articulo;
      $oArNiv2->ID_SUBNIVEL2 = $id_subnivel2;

      if (!$oArNiv2->add()) {
        $response = array("RESULT" => "1", "ERROR" => "Error agregando articulo-subnivel2");
        echo json_encode($response);
        exit();
      }
    } 

    // ARTICULOS Y SUBNIVEL 1
    if (isset($_POST['subnivel1']) && $_POST['subnivel1'] != '-1') {

      $id_subnivel1 = $_POST['subnivel1'];

      require_once('../php/class/articulo_subnivel1.class.php');
      $oArNiv1 = new articulos_subnivel1();

      $oArNiv1->ID = $oArNiv1->getlastid();
      $oArNiv1->ID_ARTICULO = $id_articulo;
      $oArNiv1->ID_SUBNIVEL1 = $id_subnivel1;

      if (!$oArNiv1->add()) {
        $response = array("RESULT" => "1", "ERROR" => "Error agregando articulo-subnivel1");
        echo json_encode($response);
        exit();
      }
    } 

    // ARTICULOS Y SUBCATEGORIAS
    if (isset($_POST['subcategoria']) && $_POST['subcategoria'] != '-1') {

      $id_subcategoria = $_POST['subcategoria'];

      require_once('../php/class/articulo_subcategoria.class.php');
      $oArSub = new articulos_subcategorias();

      $oArSub->ID = $oArSub->getlastid();
      $oArSub->ID_ARTICULO = $id_articulo;
      $oArSub->ID_SUBCATEGORIA = $id_subcategoria;

      if (!$oArSub->add()) {
        $response = array("RESULT" => "1", "ERROR" => "Error agregando articulo-subcategoria");
        echo json_encode($response);
        exit();
      }
    } 

    // ARTICULOS Y CATEGORIAS
    if (isset($_POST['categoria']) && $_POST['categoria'] != '-1') {

      $id_categoria = $_POST['categoria'];

      require_once('../php/class/articulo_categoria.class.php');
      $oArCat = new articulos_categorias();

      $oArCat->ID = $oArCat->getlastid();
      $oArCat->ID_ARTICULO = $id_articulo;
      $oArCat->ID_CATEGORIA = $id_categoria;

      if (!$oArCat->add()) {
        $response = array("RESULT" => "1", "ERROR" => "Error agregando articulo-categoria");
        echo json_encode($response);
        exit();
      }
    } 
    // ARTICULOS Y PRODCUTOS
    if (isset($_POST['producto']) && $_POST['producto'] != '-1') {

      require_once('../php/class/articulo_producto.class.php');
      $oArPro = new articulos_productos();

      $id_producto = $_POST['producto'];

      $oArPro->ID = $oArPro->getlastid();
      $oArPro->ID_ARTICULO = $id_articulo;
      $oArPro->ID_PRODUCTO = $id_producto;

      if (!$oArPro->add()) {
        $response = array("RESULT" => "1", "ERROR" => "Error agregando articulo-producto");
        echo json_encode($response);
        exit();
      }
    }



    $response = array("RESULT" => "0", "ERROR" => "NONE");
    echo json_encode($response);
    exit();


    break;

  case "DELETE_PRODUCTO":
    $id = $_POST['id'];
    require_once('../php/class/articulo_producto.class.php');
    $oArPro = new articulos_productos();
    if (!$oArPro->search($id)) {
      $response = array("RESULT" => "1", "ERROR" => "Producto no encontrado");
      echo json_encode($response);
      exit();
    } else {
      if ($oArPro->delete()) {
        $response = array("RESULT" => "0", "ERROR" => "ninguno");
        echo json_encode($response);
        exit();
      } else {
        $response = array("RESULT" => "1", "ERROR" => "error borrando en producto");
        echo json_encode($response);
        exit();
      }
    }

    break;

  case "DELETE_CATEGORIA":

    $id = $_POST['id'];
    require_once('../php/class/articulo_categoria.class.php');
    $oArCat = new articulos_categorias();
    if (!$oArCat->search($id)) {
      $response = array("RESULT" => "1", "ERROR" => "Categoria no encontrado");
      echo json_encode($response);
      exit();
    } else {
      if ($oArCat->delete()) {
        $response = array("RESULT" => "0", "ERROR" => "ninguno");
        echo json_encode($response);
        exit();
      } else {
        $response = array("RESULT" => "1", "ERROR" => "error borrando en categoria");
        echo json_encode($response);
        exit();
      }
    }

    break;

  case "DELETE_SUBCATEGORIA":

    $id = $_POST['id'];
    require_once('../php/class/articulo_subcategoria.class.php');
    $oArSubCat = new articulos_subcategorias();
    if (!$oArSubCat->search($id)) {
      $response = array("RESULT" => "1", "ERROR" => "Sub-categoria no encontrado");
      echo json_encode($response);
      exit();
    } else {
      if ($oArSubCat->delete()) {
        $response = array("RESULT" => "0", "ERROR" => "ninguno");
        echo json_encode($response);
        exit();
      } else {
        $response = array("RESULT" => "1", "ERROR" => "error borrando en sub-categoria");
        echo json_encode($response);
        exit();
      }
    }

    break;

  case "DELETE_SUBNIVEL1":

    $id = $_POST['id'];
    require_once('../php/class/articulo_subnivel1.class.php');
    $oArSubN1 = new articulos_subnivel1();
    if (!$oArSubN1->search($id)) {
      $response = array("RESULT" => "1", "ERROR" => "Sub-nivel 1 no encontrado");
      echo json_encode($response);
      exit();
    } else {
      if ($oArSubN1->delete()) {
        $response = array("RESULT" => "0", "ERROR" => "ninguno");
        echo json_encode($response);
        exit();
      } else {
        $response = array("RESULT" => "1", "ERROR" => "error borrando en sub-nivel 1");
        echo json_encode($response);
        exit();
      }
    }

    break;

  case "DELETE_SUBNIVEL2":

    $id = $_POST['id'];
    require_once('../php/class/articulo_subnivel2.class.php');
    $oArSubN2 = new articulos_subnivel2();
    if (!$oArSubN2->search($id)) {
      $response = array("RESULT" => "1", "ERROR" => "Sub-nivel 2 no encontrado");
      echo json_encode($response);
      exit();
    } else {
      if ($oArSubN2->delete()) {
        $response = array("RESULT" => "0", "ERROR" => "ninguno");
        echo json_encode($response);
        exit();
      } else {
        $response = array("RESULT" => "1", "ERROR" => "error borrando en sub-nivel 2");
        echo json_encode($response);
        exit();
      }
    }

    break;
}
?>
